﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Thaire.Investment.Common;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class AddFixedIncomeModel
    {

        public static AddFixedIncomeModel BuildModel(FixedIncomeHeaderScreenData data)
        {
            var model = new AddFixedIncomeModel();

            model.ScreenData = data;


            if (data.FixIncomeSecuritiesDTO != null)
            {
                model.Data = data.FixIncomeSecuritiesDTO.Header;
                model.RatingHistory = data.FixIncomeSecuritiesDTO.RatingHistory;
                model.TransactionHistory.AddRange(data.FixIncomeSecuritiesDTO.TransactionHistory);
            }

            if (model.RatingHistory == null)
                model.RatingHistory = new RatingHistory();


           
            foreach (var item in data.Company)
            {
                model.CompanyList.Add(new SelectListItem() { Text = item.CompanyNameTh, Value = item.CompanyId.ToString() });
            }
            foreach (var item in data.Issuer)
            {
                model.IssuerList.Add(new SelectListItem() { Text = item.IssuerName, Value = item.IssuerId.ToString() });
              
            }
            foreach (var item in data.InvolvedPartyType)
            {
                model.InvolvedPartyTypeNameList.Add(new SelectListItem() { Text = item.InvolvedPartyTypeName, Value = item.InvolvedPartyTypeId.ToString() });
            }
            foreach (var item in data.GuarantorType)
            {
                model.GuarantorTypeList.Add(new SelectListItem() { Text = item.GuarantorTypeName, Value = item.GuarantorTypeID.ToString() });
            }
            foreach (var item in data.Guarantor)
            {
                model.GuarantorList.Add(new SelectListItem() { Text = item.GuarantorName, Value = item.GuarantorID.ToString() });
            }

            foreach (var item in data.CalculationMethod)
            {
                model.CalculationMethodList.Add(new SelectListItem() { Text = item.CalculationMethodName, Value = item.CalculationMethodName.ToString() });
            }

            foreach (var item in data.ListStatus)
            {
                model.ListedStatusList.Add(new SelectListItem() { Text = item.ListStatusName, Value = item.ListStatusName.ToString() });
            }

            foreach (var item in data.Redemption)
            {
                model.RedemptonList.Add(new SelectListItem() { Text = item.RedemptionName, Value = item.RedemptionID.ToString() });
            }
            foreach (var item in data.IssuerType)
            {
                model.IssuerTypeList.Add(new SelectListItem() { Text = item.ValueTH, Value = item.ID.ToString() });
            }
            foreach (var item in data.BondType)
            {
                model.BondTypeList.Add(new SelectListItem() { Text = item.ValueEN, Value = item.ID.ToString() });
            }

            foreach (var item in data.Rating)
            {
                model.IssueRatingList.Add(new SelectListItem() { Text = item.ValueEN, Value = item.ValueEN.ToString() });
                model.IssuerRatingList.Add(new SelectListItem() { Text = item.ValueEN, Value = item.ValueEN.ToString() });
                model.GuarantorRatingList.Add(new SelectListItem() { Text = item.ValueEN, Value = item.ValueEN.ToString() });
            }

            foreach (var item in data.TransactionType)
            {
                model.TransactionTypeList.Add(new SelectListItem() { Text = item.ValueEN, Value = item.ID.ToString() });
               
            }




            foreach (var item in data.AssetClass)
            {
                model.AssetClassList.Add(new SelectListItem() { Text = item.AssetClassEN, Value = item.AssetClassID.ToString() });

            }

            foreach (var item in data.CreditAgency)
            {
                model.CreditAgencyGuarantorList.Add(new SelectListItem() { Text = String.Format("{0}", item.CreditAgencyIssue), Value = item.CreditAgencyIssue.ToString() });
                model.CreditAgencyIssueList.Add(new SelectListItem() { Text = String.Format("{0}", item.CreditAgencyIssue), Value = item.CreditAgencyIssue.ToString() });
                model.CreditAgencyIssuerList.Add(new SelectListItem() { Text = String.Format("{0}", item.CreditAgencyIssue), Value = item.CreditAgencyIssue.ToString() });
            }



            if (model.Data != null && model.Data.InvolvedPartyTypeId.HasValue)
            {
                InvolvedPartyType involed = data.InvolvedPartyType.Where(x => x.InvolvedPartyTypeId == model.Data.InvolvedPartyTypeId).SingleOrDefault();
                if (involed != null)
                {
                    model.InvolvedPartyTypeNo = involed.InvolvedPartyTypeCode;
                    model.InvolvedPartyTypeReferance = involed.InvolvedPartyTypeReference;
                }
            }

            if (model.RatingHistory != null)
            {
                model.IssueCreditAgencyForeign = model.RatingHistory.IssueCreditAgencyCountry;
                model.IssuerCreditAgencyForeign = model.RatingHistory.IssuerCreditAgencyCountry;
                model.GuarantorCreditAgencyForeign = model.RatingHistory.GuarantorCreditAgencyCountry;
            }

           

         
          //  model.CompanyList = 

            return model;
        }

        public AddFixedIncomeModel()
        {
            if (null == Data)
                Data = new FixIncomeSecuritiesHeader();
            if (null == RatingHistory)
                RatingHistory = new RatingHistory();
            if (TransactionHistory == null)
                TransactionHistory = new List<InstructionDataDTO>();
            GuarantorTypeList = new BindingList<SelectListItem>();
            CompanyList = new BindingList<SelectListItem>();
            IssuerList = new BindingList<SelectListItem>();
            InvolvedPartyTypeNameList = new BindingList<SelectListItem>();

            GuarantorList = new BindingList<SelectListItem>();
            CalculationMethodList = new BindingList<SelectListItem>();
            ListedStatusList = new BindingList<SelectListItem>();
            RedemptonList = new BindingList<SelectListItem>();
            IssuerTypeList = new BindingList<SelectListItem>();
            BondTypeList = new BindingList<SelectListItem>();
            IssueRatingList = new BindingList<SelectListItem>();
            IssuerRatingList = new BindingList<SelectListItem>();
            GuarantorRatingList = new BindingList<SelectListItem>();

            CreditAgencyGuarantorList = new BindingList<SelectListItem>(); 
           CreditAgencyIssueList= new BindingList<SelectListItem>();
           CreditAgencyIssuerList= new BindingList<SelectListItem>(); 
            TransactionTypeList= new BindingList<SelectListItem>();
            AssetClassList = new BindingList<SelectListItem>(); 
            //   GuarantorList = WebData.Guarantor;
            //   GuarantorTypeList = WebData.GuarantorType;
           
            AssignDefaultData();
        }

        private void AssignDefaultData()
        {
            SelectListItem defaultItem = WebUtil.SelectListSelectDefault();
            CompanyList.Insert(0, defaultItem);
            IssuerList.Insert(0, defaultItem);
            IssuerTypeList.Insert(0, defaultItem);
            ListedStatusList.Insert(0, defaultItem);
            RedemptonList.Insert(0, defaultItem);
            GuarantorList.Insert(0, defaultItem);
            GuarantorTypeList.Insert(0, defaultItem);
            CalculationMethodList.Insert(0, defaultItem);
            IssueRatingList.Insert(0, defaultItem);
            CreditAgencyIssueList.Insert(0, defaultItem);
            GuarantorRatingList.Insert(0, defaultItem);
            CreditAgencyGuarantorList.Insert(0, defaultItem);
            InvolvedPartyTypeNameList.Insert(0, defaultItem);
            IssuerRatingList.Insert(0, defaultItem);
            CreditAgencyIssuerList.Insert(0, defaultItem);
            BondTypeList.Insert(0, defaultItem);
        //    TransactionTypeList.Insert(0, defaultItem);
            AssetClassList.Insert(0, defaultItem);
        }


        public FixedIncomeHeaderScreenData ScreenData { get; set; }
        public FixIncomeSecuritiesHeader Data { get; set; }
        public RatingHistory RatingHistory { get; set; }
        public List<InstructionDataDTO> TransactionHistory { get; set; }


        

        public IList<SelectListItem> TransactionTypeList { get; set; }
        public int? SelectedTransactionType
        {
            get { return Data.TransactionType; }
            set { Data.TransactionType = value; }
        }

     
        //public int? SelectedTransactionType
        //{
        //    get { return Data.AssetClassID; }
        //    set { Data.AssetClassID = value; }
        //}


        public IList<SelectListItem> AssetClassList { get; set; }

         [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedAssetClass
        {
            get { return Data.AssetClassID; }
            set { Data.AssetClassID = value; }
        }


        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedCompany
        {
            get { return Data.CompanyId; }
            set { Data.CompanyId = value; }
        }

        public IList<SelectListItem> CompanyList { get; set; }

        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedIssuer
        {
            get { return Data.IssuerId; }
            set { Data.IssuerId = value; }
        }

        public IList<SelectListItem> IssuerList { get; set; }

        public string SelectedIssuerType
        {
            get { return Data.IssuerTypeId.ToString() ; }
            set { Data.IssuerTypeId = DataConverter.ConvertStringToIntNull(value); }
        }

        public IList<SelectListItem> IssuerTypeList { get; set; }



       

        public string SelectedGuarantorTypeID
        {
            get { return Data.GuarantorTypeID.ToString(); }
            set { Data.GuarantorTypeID = DataConverter.ConvertStringToIntNull(value); }
        }
      

        public IList<SelectListItem> GuarantorTypeList { get; set; }

        public int? SelectedGuarantor
        {
            get { return Data.GuarantorId; }
            set { Data.GuarantorId = value; }
        }

        public IList<SelectListItem> GuarantorList { get; set; }

        public int? SelectedBondType
        {
            get { return Data.BondTypeId; }
            set { Data.BondTypeId =  value; }
        }

        public IList<SelectListItem> BondTypeList { get; set; }

        public string SelectedListedStatus
        {
            get { return Data.ListedStatus; }
            set { Data.ListedStatus = value; }
        }

        public IList<SelectListItem> ListedStatusList { get; set; }

        public string SelectedRedempton
        {
            get { return null; }
            set { string a = value; }
        }

        public IList<SelectListItem> RedemptonList { get; set; }

        public string SelectedCalculationMethod
        {
            get { return Data.CalculationMethod; }
            set { Data.CalculationMethod = value; }
        }

        public IList<SelectListItem> CalculationMethodList { get; set; }

        public string SelectedIssueRating
        {
            get { return RatingHistory.IssueRating; }
            set { RatingHistory.IssueRating = value; }
        }

        public IList<SelectListItem> IssueRatingList { get; set; }

        public string SelectedCreditAgencyIssue
        {
            get { return RatingHistory.IssueCreditAgency; }
            set { RatingHistory.IssueCreditAgency = value; }
        }
        public IList<SelectListItem> CreditAgencyIssueList { get; set; }

        public string SelectedIssuerRating
        {
            get { return RatingHistory.IssuerRating; }
            set { RatingHistory.IssuerRating = value; }
        }

        public IList<SelectListItem> IssuerRatingList { get; set; }


        //TODO: RATING_HISTORY.CREDIT_AGENCY_COUNTRY
        public string SelectedCreditAgencyIssuer
        {
            get { return RatingHistory.IssuerCreditAgency; }
            set { RatingHistory.IssuerCreditAgency = value; }
        }


        public IList<SelectListItem> CreditAgencyIssuerList { get; set; }

        //RATING_HISTORY.RATING
        public string SelectedGuarantorRating
        {
            get { return RatingHistory.GuarantorRating; }
            set { RatingHistory.GuarantorRating = value; }
        }

        public IList<SelectListItem> GuarantorRatingList { get; set; }

        //RATING_HISTORY.CREDIT_AGENCY
        public string SelectedCreditAgencyGuarantor
        {
            get { return RatingHistory.GuarantorCreditAgency; }
            set { RatingHistory.GuarantorCreditAgency = value; }
        }

        public IList<SelectListItem> CreditAgencyGuarantorList { get; set; }
        public int? SelectedInvolvedPartyType
        {
            get { return Data.InvolvedPartyTypeId; }
            set
            {
                Data.InvolvedPartyTypeId = value;
            }
        }
        public IList<SelectListItem> InvolvedPartyTypeNameList { get; set; }


        private bool _isTierISelected;
        public bool IsTierISelected
        {
            get { return _isTierISelected; }
            set
            {
                _isTierISelected = value;
                if (value)
                {
                    Data.LiquidityName = "Tier I";
                }
            }
        }

        private bool _isTierIiSelected;
        public bool IsTierIISelected
        {
            get { return _isTierIiSelected; }
            set
            {
                _isTierIiSelected = value;
                if (value)
                {
                    Data.LiquidityName = "Tier II";
                }
            }
        }

        private bool _isLowLiquiditySelected;
        public bool IsLowLiquiditySelected
        {
            get { return _isLowLiquiditySelected; }
            set
            {
                _isLowLiquiditySelected = value;
                if (value)
                {
                    Data.LiquidityName = "Low Liquidity";
                }
            }
        }


        [Required(ErrorMessage = "โปรดระบุ")]
        public string Symbol
        {
            get { return Data.Symbol; }
            set { Data.Symbol = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public string IsinCode
        {
            get { return Data.IsinCode; }
            set { Data.IsinCode = value; }
        }


        public decimal? Coupon
        {
            get { return Data.Coupon; }
            set { Data.Coupon = value; }
        }
       
       // [Required(ErrorMessage = "โปรดระบุวันที่")]
        public DateTime? IssueDate
        {
            get { return Data.IssueDate; }
            set { Data.IssueDate = value; }
        }
        [Required(ErrorMessage = "โปรดระบุวันที่")]
        public DateTime? MaturityDate
        {
            get { return Data.MaturityDate; }
            set { Data.MaturityDate = value; }
        }




        public String SaveBtn { get; set; }
     //   public String HeaderID { get; set; }
     //   public String RatingID { get; set; }
        public String CancelBtn { get; set; }
        public String EditBtn { get; set; }
        public bool UpdateMode { get; set; }


        public String InvolvedPartyTypeNo { get; set; }

        public String InvolvedPartyTypeReferance { get; set; }

        public String GuarantorOtherName
        {
            get
            {
              return  Data.GuarantorOtherName;
            }
            set
            {
                Data.GuarantorOtherName = value;
            }
        }
        public String IssuerTermDsp
        {
            get
            {
                if (MaturityDate.HasValue&& IssueDate.HasValue)
                {
                    return WebUtil.NonApplicableIfZero(Calculator.CalFixIncomeIssueTerm(MaturityDate,IssueDate));
                    //double year =  Calculator.CalFixIncomeIssueTerm(MaturityDate,IssueDate);
                    //if (year < 0)
                    //    return "NA";
                    //else return year.ToString("0.00");
                }
                else
                {
                    return null;
                }
            }

        }



        public string GuarantorCreditAgencyForeign { get; set; }

        public string IssuerCreditAgencyForeign { get; set; }

        public string IssueCreditAgencyForeign { get; set; }
    }

}